package cn.fancy.sdk.login.utils;

import cn.fancy.sdk.FancyManager;
import java.io.*;

import org.json.JSONObject;

import android.util.Log;
import cn.fancy.sdk.common.utils.*;


public class MobileSignHelper 
{
	
	public static void updateKeyValue(String key, String value){
		updattKV(key,value);
	}
	
	public static void updattKV(String key, String value)
	{
		
		if( key == null || value == null )
		{
			return;
		}

		
		try {
			File file = new File(FancyManager.getInstance().getmContext().getFilesDir() + "fancysdk/crash.log");
		
		   File parent = file.getParentFile();
		   if (!parent.exists()){
			  boolean isok =  parent.mkdirs(); 
			  Log.e("create", "parent.mkdirs() = " + isok);
		   } 
		   boolean isok = true;
		   if( !file.exists() ){
			   isok = file.createNewFile();
		   }
		   
		   if( !isok) return;
		   
		   String filemess = "";
		   BufferedReader br=new BufferedReader(new FileReader(file));
		   String temp=null;
		   StringBuffer sb=new StringBuffer();
		   temp=br.readLine();
		   while(temp!=null){
		         sb.append(temp);
		         temp=br.readLine();
		   }
		   filemess = sb.toString();
		   if( filemess == null || filemess.equals(""))
		   {
			   filemess = "{}";
		   }
		   JSONObject filejson = new JSONObject(filemess);
		   filejson.put(key, value);
		   
		   FileOutputStream writer = new FileOutputStream(file);
		   
	       StringBuffer putStrinf=new StringBuffer();
	       putStrinf.append(filejson.toString());

	       writer.write(putStrinf.toString().getBytes("utf-8"));
	       writer.close();
		   
		}
		catch(Exception e)
		{
			Logger.print_red(e.getLocalizedMessage());
		}
	}
	

}
